﻿--[[
   @brief    product manager.
   @author   hqzheng
   @version  1.0
   @date     2018-03-21
--]]

product_mgr = 
{   
    -- 驱动模块加载的事件
    events = {};
    -- 事件ID到模块集合的映射
    event_2_module_map = {};
    -- 产品信息文件
    product_info = {
        WEGAME_ZH = "assemblage_info\\products\\wegame_zh.xml",
        WEGAME_OVERSEA = "assemblage_info\\products\\wegame_oversea.xml",
    };
    -- 产品ID定义
    product_id_define = {
        WEGAME_ZH_ID = 0,      -- 国内个人版本
        WEGAME_ZH_NET_ID = 1,  -- 国内网吧版本
        WEGAME_OVERSEA_ID = 2, -- 国际版本
    };
    
    init = function (args)
        local path = product_mgr.product_info.WEGAME_ZH; 
        base_utility.log(string.format("[product_mgr]path:%s", path));
        local channel_id_data = comm_center.call_service("i_service_get_channel_id", {});
        if nil ~= channel_id_data and channel_id_data.channel_id == product_mgr.product_id_define.WEGAME_OVERSEA_ID then
            path = product_mgr.product_info.WEGAME_OVERSEA;
        end
        if false == product_mgr.proc_product_info_file(path) then
            product_mgr.fix();
        end
        product_mgr.register_event();
    end;

    -- 处理产品信息配置文件，生成事件ID到模块集合的映射表
    proc_product_info_file = function (xml_path)
        local xml = newParser();
        local xmlparser = xml:loadVfsFile(xml_path);
        if nil == xmlparser or nil == xmlparser.loadinfo then
            return false;
        end
        local is_success = true;
        local node_size = #xmlparser.loadinfo.event;
        if nil == node_size then
            is_success = false;
        end

        if 0 == node_size then
            local event = xmlparser.loadinfo.event;
            local event_id = event["@id"];
            table.insert(product_mgr.events, event_id);
            local assemble_size = #event.assemble;
            local module_lists_tb = {};
            for j = 1, assemble_size do
                local assemble_file_name = event.assemble[j]["@create"];
                product_mgr.proc_moudles_list_file(assemble_file_name, module_lists_tb);
            end
            product_mgr.event_2_module_map[event_id] = module_lists_tb;
            if 0 == #module_lists_tb then
                is_success = false;
            end
        else
            for i = 1, node_size do
                local event = xmlparser.loadinfo.event[i];
                local event_id = event["@id"];
                table.insert(product_mgr.events, event_id);
                local assemble_size = #event.assemble;
                local module_lists_tb = {};
                for j = 1, assemble_size do
                    local assemble_file_name = event.assemble[j]["@create"];
                    product_mgr.proc_moudles_list_file(assemble_file_name, module_lists_tb);
                end
                product_mgr.event_2_module_map[event_id] = module_lists_tb;
                if 0 == #module_lists_tb then
                    is_success = false;
                end
            end;
        end
        return is_success;
    end;

    -- 读取模块集合文件构造模块集合table
    proc_moudles_list_file = function (file_name, module_list)
        if nil == file_name then
            return false;
        end
        local file_path = "assemblage_info\\" .. file_name;
        local xml = newParser();
        local xmlparser = xml:loadVfsFile(file_path);
        if nil == xmlparser or nil == xmlparser.assemble then
            return false;
        end

        local node_size = #xmlparser.assemble.module;
        if 0 == node_size then
            local module_name = xmlparser.assemble.module["@name"];
            table.insert(module_list, module_name);
        else
            for i = 1, node_size do
                local module_name = xmlparser.assemble.module[i]["@name"];
                table.insert(module_list, module_name);
            end
        end
        return true;
    end;

    -- 注册需要驱动模块加载的事件
    register_event = function ()
        local event_count = #product_mgr.events;
        for i = 1, event_count do
            comm_center.listen_broadcast(product_mgr.events[i], "product_mgr.handle_event");
        end
    end;

    -- 加载事件对应的模块
    handle_event = function (data)
        if nil == data then
            return;
        end
        local module_lists_tb = product_mgr.event_2_module_map[data.cmd];
        local module_list = ierd_tgp.tpf_ui.LuaModuleList:new();
        for i = 1, #module_lists_tb do
            local module_name = ierd_tgp.tpf_ui.LuaModuleName:new();
            module_name.module_name = module_lists_tb[i];
            module_list.module_list[i-1] = module_name;
            module_name:delete();
        end
        module_list.module_count = #module_lists_tb;
        ierd_tgp.tpf_ui.LoadDesc(module_list);
        desc_mgr.work();
        module_list:delete();
    end;

    -- 发生异常，使用旧的方式加载
    fix = function ()
        base_utility.log("[product_mgr]load xml fail, fix it!", true);
        ierd_tgp.tpf_ui.LoadDesc();
        desc_mgr.work();
    end;
}

product_mgr.init();